package com.ding.demo.service1.config;

import com.ding.demo.service1.TestJob;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;

import java.io.IOException;
import java.util.Properties;

/**
 * 配置任务调度中心
 * [QRTZ_JOB_DETAILS], [QRTZ_TRIGGERS] and [QRTZ_CRON_TRIGGERS]
 * <p>
 * <p>
 * <p>
 * test
 *
 * @author lance
 */
@Configuration
public class QuartzConfig {


   /* @Value("${quartzread.url}")
    private String url;
    @Value("${quartzread.username}")
    private String userName;
    @Value("${quartzread.password}")
    private String passWord;
    @Value("${quartzread.driver-class-name}")
    private String driverClassName;
*/

    @Autowired
    private JobFactory jobFactory;

    @Bean
    public Scheduler scheduler() throws IOException, SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.setJobFactory(jobFactory);
        scheduler.start();
        return scheduler;
    }

    @Bean
      public JobDetailFactoryBean jobDetailFactoryBean() {
                JobDetailFactoryBean factory = new JobDetailFactoryBean();
                 factory.setJobClass(TestJob.class);
                 return factory;
             }

    /**
     * 设置quartz属性
     *
     * @throws IOException 2016年10月8日下午2:39:05
     */
    public Properties quartzProperties() throws IOException {
        Properties prop = new Properties();
        prop.put("org.quartz.scheduler.instanceName", "JdScheduler");
        prop.put("org.quartz.scheduler.instanceId", "AUTO");
        prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
        prop.put("org.quartz.scheduler.instanceId", "NON_CLUSTERED");
        prop.put("org.quartz.scheduler.jobFactory.class", "org.quartz.simpl.SimpleJobFactory");
        prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
/*
        prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
*/
       /* prop.put("org.quartz.jobStore.dataSource", "quartzDataSource");*/
        /*  prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");*/
        //是否启用集群，启用，改为true
        prop.put("org.quartz.jobStore.isClustered", "true");
        //JobStore 能处理的错过触发的 Trigger 的最大数量
        prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "200");
        //集群中服务器相互检测间隔，每台服务器都会按照下面配置的时间间隔往服务器中更新自己的状态，如果某台服务器超过以下时间没有checkin，调度器就会认为该台服务器已经down掉，不会再分配任务给该台服务器
        prop.put("org.quartz.jobStore.clusterCheckinInterval", "20000");
        prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        prop.put("org.quartz.threadPool.threadCount", "300");

//        prop.put("org.quartz.dataSource.quartzDataSource.driver", "com.mysql.jdbc.Driver");
//        prop.put("org.quartz.dataSource.quartzDataSource.URL", "jdbc:mysql://10.135.30.137:5637/base_system?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=UTF8&useSSL=false&allowMultiQueries=true");
//        prop.put("org.quartz.dataSource.quartzDataSource.user", "root");
//        prop.put("org.quartz.dataSource.quartzDataSource.password", "Haier,123");


       /* prop.put("org.quartz.dataSource.quartzDataSource.driver", driverClassName);
        prop.put("org.quartz.dataSource.quartzDataSource.URL", url);
        prop.put("org.quartz.dataSource.quartzDataSource.user",userName);
        prop.put("org.quartz.dataSource.quartzDataSource.password", passWord);
*/

//        prop.put("org.quartz.dataSource.quartzDataSource.URL", "jdbc:mysql://localhost:3306/base_system?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=UTF8&useSSL=false&allowMultiQueries=true");
//        prop.put("org.quartz.dataSource.quartzDataSource.user", "root");
//        prop.put("org.quartz.dataSource.quartzDataSource.password", "123456");
        /*prop.put("org.quartz.dataSource.quartzDataSource.maxConnections", "200");
        prop.put("org.quartz.dataSource.quartzDataSource.idleConnectionValidationSeconds", "60");
        prop.put("org.quartz.dataSource.quartzDataSource.validateOnCheckout", "true");
        prop.put("org.quartz.dataSource.quartzDataSource.validationQuery", "select 1");*/
        return prop;
    }
}